package cdc.javafx;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;

import cdc.layout.IPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class DataTool {
	
	public static LinkedList<Scene> scenceLists=new LinkedList<>();
	
	private static DataTool instance=null;
	
	public static DataTool instance() {
		if(instance==null){
            synchronized (DataTool.class){
                if(instance==null){
                    instance=new DataTool();
                }
            }
        }
        return instance;
	}
	private DataTool() {
		
	}
	
	public Image getImage(String path) {
        Image ICON_48 = new Image(getClass().getResourceAsStream(path));
        return ICON_48;
	}
	
	public Image getImage() {
		String IMAGE = "../../images/icon-88x88.png";
        return getImage(IMAGE);
	}
	
	public Image getBackImage() {
		String IMAGE = "../../images/back_16.png";
        return getImage(IMAGE);
	}
	
	public Image getCodeImage() {
		String IMAGE = "../../images/codea_16.png";
        return getImage(IMAGE);
	}
	
	public Image getHomeImage() {
		String IMAGE = "../../images/home_16.png";
        return getImage(IMAGE);
	}
	
	private Scene newScence=null;
	
	public Button createButton(Stage stage,Scene fatherScene,Class<? extends IPane> clz) {
		Button button=null;
		try {
			String clzName=clz.getSimpleName();
			if(clzName.contains("_")) {
				clzName=clzName.substring(0,clzName.indexOf("_"));
			}
		    button = new Button(clzName);
			IPane ipane=clz.newInstance();
			Method m = clz.getDeclaredMethod("createPane",new Class[] {Stage.class,Scene.class}); 
			Pane pane=(Pane) m.invoke(ipane,stage,fatherScene); 
			button.setPrefWidth(200);
	        button.setOnAction(new EventHandler<ActionEvent>() {
	            @Override
	            public void handle(ActionEvent event) {
	                stage.setTitle(clz.getSimpleName());
	                pane.setPrefSize(1024, 768);
	                if(newScence==null) {
	                	newScence = new Scene(pane);
	                	DataTool.scenceLists.addFirst(newScence);
	                }
	                stage.setScene(newScence);
	            }
	        });
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return button;
		
	}
	

}
